#include "unity.h"
#include "proc_eth.h"
#include "mock_main.h"  // 包含所有需要mock的函数


// Test variables
Eth_Service testEthService;
B1553_BusService testBusService;
u32 test_current_mode;

void setUp(void) {
    // Initialize test structures
    memset(&testEthService, 0, sizeof(Eth_Service));
    memset(&testBusService, 0, sizeof(B1553_BusService));
    test_current_mode = 0;
}

void tearDown(void) {
    // Cleanup after each test
}

/**
 * TC001: Test when rx_flag is zero
 * Expected: Function returns immediately without any processing
 */
void test_proc_eth_when_rx_flag_is_zero_should_return_immediately(void) {
    // Arrange
    testEthService.rx_flag = 0;
    
    // Act
    proc_eth(&testEthService, &testBusService, test_current_mode);
    
    // Assert
    // No functions should be called
    TEST_ASSERT_EQUAL(0, testEthService.rx_flag);
}

/**
 * TC002: Test in work mode
 * Expected: Reset FIFO and decrement rx_flag, then return
 */
void test_proc_eth_in_work_mode_should_reset_fifo_and_decrement_rx_flag(void) {
    // Arrange
    testEthService.rx_flag = 5;
    testEthService.Fifo_InstancePtr = (void*)0x12345678;
    test_current_mode = SYS_MODE_WORK;
    
    // Expect XLlFifo_RxReset to be called with the FIFO instance pointer
    XLlFifo_RxReset_Expect(testEthService.Fifo_InstancePtr);
    
    // Act
    proc_eth(&testEthService, &testBusService, test_current_mode);
    
    // Assert
    TEST_ASSERT_EQUAL(4, testEthService.rx_flag); // Should be decremented
}

/**
 * TC003: Test ICMPv6 packet handling
 * Expected: Process ICMPv6 packet and increment telemetry count
 */
void test_proc_eth_with_icmpv6_packet_should_handle_icmpv6_and_increment_tm_count(void) {
    // Arrange
    testEthService.rx_flag = 3;
    test_current_mode = 0; // Not work mode
    
    // Expectations
    eth_FifoRecvData_ExpectAndReturn(&testEthService, 0);
    parse_Rxpacket_ExpectAnyArgsAndReturn(ETH_FRAME_ICMPV6);
    B1553_TMadd_Comm_Eth_TX_Count_Expect(&testBusService);
    
    // Act
    proc_eth(&testEthService, &testBusService, test_current_mode);
    
    // Assert
    TEST_ASSERT_EQUAL(2, testEthService.rx_flag); // Should be decremented
}

/**
 * TC004: Test UDP packet handling (not complete update)
 * Expected: Process UDP packet, update counters, set updating status
 */
void test_proc_eth_with_udp_packet_when_not_complete_should_process_udp_and_set_updating(void) {
    // Arrange
    testEthService.rx_flag = 2;
    testEthService.update_expect_cnt = 100; // Less than 256
    test_current_mode = 0; // Not work mode
    
    u8 testData[100] = {0};
    u32 testDataLen = 0;
    
    // Expectations
    eth_FifoRecvData_ExpectAndReturn(&testEthService, 0);
    
    parse_Rxpacket_ExpectAnyArgsAndReturn(ETH_FRAME_UDP);

    // u32 parse_Rxpacket(Eth_Service *pEthSer, u8 *rcvbuf, u32 *size)
    // parse_Rxpacket_ExpectAndReturn(&testEthService, 0, &testDataLen, ETH_FRAME_UDP);
    // parse_Rxpacket_IgnoreArg_rcvbuf();
    // parse_Rxpacket_ReturnThruPtr_rcvbuf(testData);

    uart_rs01_send_Expect(testData, testDataLen); // This might need adjustment based on actual data
    B1553_TMadd_Comm_Update_Count_Expect(&testBusService);
    B1553_TMset_Update_stat_Expect(&testBusService, PROGRAM_UPDATING);
    
    // Act
    proc_eth(&testEthService, &testBusService, test_current_mode);
    
    // Assert
    TEST_ASSERT_EQUAL(1, testEthService.rx_flag); // Should be decremented
    TEST_ASSERT_EQUAL(101, testEthService.update_expect_cnt); // Should be incremented
}

/**
 * TC005: Test UDP packet handling (complete update)
 * Expected: Process UDP packet, mark update complete and send telemetry
 */
void test_proc_eth_with_udp_packet_when_complete_should_mark_done_and_send_tm(void) {
    // Arrange
    testEthService.rx_flag = 1;
    testEthService.update_expect_cnt = 255; // Next will be 256
    test_current_mode = 0; // Not work mode
    
    u8 testData[100] = {0};
    u32 testDataLen = 0;
    
    // Expectations
    eth_FifoRecvData_ExpectAndReturn(&testEthService, 0);
    parse_Rxpacket_ExpectAnyArgsAndReturn(ETH_FRAME_UDP);
    uart_rs01_send_Expect(testData, testDataLen); // This might need adjustment
    B1553_TMadd_Comm_Update_Count_Expect(&testBusService);
    B1553_TMset_Update_stat_Expect(&testBusService, PROGRAM_UPDATE_DONE);
    uart_rs01_sendTM_Expect();
    uart_rs01_rcvTM_Expect();
    
    // Act
    proc_eth(&testEthService, &testBusService, test_current_mode);
    
    // Assert
    TEST_ASSERT_EQUAL(0, testEthService.rx_flag); // Should be decremented to 0
    TEST_ASSERT_EQUAL(0, testEthService.update_expect_cnt); // Should be reset to 0
}

/**
 * TC006: Test error condition handling
 * Expected: Update telemetry status with error code
 */
void test_proc_eth_with_error_should_update_tm_status(void) {
    // Arrange
    testEthService.rx_flag = 4;
    test_current_mode = 0; // Not work mode
    u32 error_code = 0x1234; // Simulate error code
    
    // Expectations
    eth_FifoRecvData_ExpectAndReturn(&testEthService, 0);
    parse_Rxpacket_ExpectAnyArgsAndReturn(error_code);
    B1553_TMset_Update_stat_Expect(&testBusService, error_code);
    
    // Act
    proc_eth(&testEthService, &testBusService, test_current_mode);
    
    // Assert
    TEST_ASSERT_EQUAL(3, testEthService.rx_flag); // Should be decremented
}

/**
 * TC007: Test parse returning 0 (no specific frame type)
 * Expected: No additional actions taken except decrementing rx_flag
 */
void test_proc_eth_with_parse_returning_zero_should_only_decrement_rx_flag(void) {
    // Arrange
    testEthService.rx_flag = 2;
    test_current_mode = 0; // Not work mode
    
    // Expectations
    eth_FifoRecvData_ExpectAndReturn(&testEthService, 0);
    parse_Rxpacket_ExpectAnyArgsAndReturn(0);
    // No other functions should be called
    
    // Act
    proc_eth(&testEthService, &testBusService, test_current_mode);
    
    // Assert
    TEST_ASSERT_EQUAL(1, testEthService.rx_flag); // Should be decremented
}

